package com.shi.desktop.indexer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.nio.charset.Charset;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

public class PlainTextHandler implements DocumentHandler {

	private Document doc;

	public Document getDocument(InputStream is) throws DocumentHandlerException {

		String bodyText = "";
		//Charset charset = Charset.defaultCharset();
		Charset charset = Charset.forName("GBK");
		try {
			PushbackInputStream pis = new PushbackInputStream(is, 3);
			byte[] b = new byte[3];
			pis.read(b);
			
			if(((b[0]&0xff) == 0xff) && ((b[1]&0xff) == 0xfe)) {
				charset = Charset.forName("UTF-16LE");
				pis.unread(b[2]);
				//System.out.println("Unicode");
			}
			else if(((b[0]&0xff) == 0xfe) && ((b[1]&0xff) == 0xff)) {
				charset = Charset.forName("UTF-16BE");
				pis.unread(b[2]);
				//System.out.println("BE-Unicode");
			}
			else if(((b[0]&0xff) == 0xef) && ((b[1]&0xff) == 0xbb) && ((b[2]&0xff) == 0xbf)) {
				charset = Charset.forName("UTF-8");
				//System.out.println("utf-8");
			}
			else {
				pis.unread(b);
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(pis, charset));
			String line = null;
			while ((line = br.readLine()) != null) {
				bodyText += line;
			}
			br.close();
		} catch (IOException e) {
			throw new DocumentHandlerException("Cannot read the text document",
					e);
		}
		
		if (!bodyText.equals("")) {
			doc = new Document();
			doc.add(new Field("content", bodyText, Field.Store.NO,
					Field.Index.ANALYZED));
			return doc;
		}
		return null;
	}
}
